package cc.chenwenxi.mypi.appium;

import cc.chenwenxi.mypi.appium.entity.app.快手极速;
import cc.chenwenxi.mypi.appium.kits.AppiumUtils;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import jodd.util.Wildcard;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Slf4j
public class 快手极速版AppiumTask implements AppiumTask {
    private final String pre = "快手极速版_";

    private final AndroidDriver driver;
    private final AppiumUtils kit;


    public 快手极速版AppiumTask(AndroidDriver driver) {
        this.driver = driver;
        this.kit = new AppiumUtils(driver);
    }

    @Override
    public boolean isNeedRun() {
        return true;
    }

    @Override
    public String appName() {
        return "快手极速版";
    }

    @Override
    public void run(Date endTime) {
        while(true){
            try {
                if (endTime.before(new Date())) {
                    log.info("运行结束!!,过期时间为:{}", DateUtil.formatDateTime(endTime));
                    break;
                }
                this.main();
            } catch (Exception e) {
                log.error("异常,跳出本循环", e);
                break;
            }finally {
                int i = RandomUtil.randomInt(1, 3);
                log.info("间隔:{}", i);
                ThreadUtil.sleep(i, TimeUnit.SECONDS);
            }
        }
    }

    public void main(){
        快手极速.导航 导航 = new 快手极速.导航(driver);
        AndroidElement 首页导航 = 导航.get首页导航();

        AndroidElement 剩余时间 = 导航.get剩余时间();
        String textTime = 剩余时间.getText();
        log.info("点击宝箱时间:{}",textTime);
//        if (Wildcard.matchPath(textTime,"得**")) {
//            //需要进行点击去赚钱
//            log.info("点击_去赚钱");
//            kit.click(剩余时间,0,0);
//            ThreadUtil.sleep(5, TimeUnit.SECONDS);
//
//        }else{
            // 继续刷视频
            if (!首页导航.isSelected()) {
                首页导航.click();
            }
            kit.swipeUp();
            log.info("{}上滑", pre);
            快手极速.首页 首页 = new 快手极速.首页(driver);
            if (RandomUtil.randomBoolean() || true) {
                log.info("点心");
//                ThreadUtil.sleep(1, TimeUnit.SECONDS);
//                首页.get点心().click();
                kit.click(首页.get点心(),0,0);
            }
//        }

    }

    @Override
    public void openActivity() {
        driver.startActivity(快手极速.mainActivity);
    }

    @Override
    public void checkAndInstallApk() {
//        if (!driver.isAppInstalled(快手极速.mainActivity.getAppPackage())) {
//            log.info("{}没有安装正在安装中", pre);
//            try {
//                driver.pushFile("/sdcard/kuaishou_jisu.apk",new File("/home/data/apks/kuaishou_jisu.apk"));
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            driver.installApp("/sdcard/kuaishou_jisu.apk");
//        }
    }
}
